/************************************************************
 * $Id : CCmdJoin.cpp   2011/08/01
 *
 *       swhors@naver.com
 ************************************************************/

#include "config.h"

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>

using namespace std;

#include "constant.h"
#include "ast_peg.h"
#include "ast_db.h"
#include "CmdBase.h"
#include "CmdJoin.h"

#include "ast_event.h"

extern char  gOutgoingFolder[256];
extern int   gIsConnected2DB;

extern int sendCommand( char *aFmt, ...);

CCmdJoin::CCmdJoin()
{
   init( );
}

CCmdJoin::~CCmdJoin()
{
   init( );
}

void CCmdJoin::release()
{
   super::release();
}

void CCmdJoin::init()
{
   super::init();
   
   memset( mConferenceName, 0, 32 );
   memset( mUniqueId,       0, 32 );
   memset( mFlags,          0, 32 );
   memset( mCallerId,       0, 32 );
   memset( mCallerIdName ,  0, 32 );
   
   mCount  = 0;
   mMember = 0;
}

void *CCmdJoin::parse( char * aCommandStr )
{
   char * sStart       = NULL;
   char * sMsgStartPtr = NULL;
   char   sBuff[32]    = {0,};
   
   sMsgStartPtr = aCommandStr;
   
   super::parse( sMsgStartPtr );
   
   sMsgStartPtr = aCommandStr;
   
   if ( conferencename_f::Matches( sMsgStartPtr, sStart ) )
   {
       memset( mConferenceName, 0, 32 ); 
       memcpy( mConferenceName, sStart + 16, ( sMsgStartPtr - sStart - 18) );
   }
   else
   {
      /* do nothing */
   }
   
   if ( uniqueid_f::Matches( sMsgStartPtr, sStart ) )
   {
       memset( mUniqueId, 0, 32 ); 
       memcpy( mUniqueId, sStart + 10, ( sMsgStartPtr - sStart - 12) );
   }
   else
   {
      /* do nothing */
   }

   if ( member_f::Matches( sMsgStartPtr, sStart ) )
   {
      memset(sBuff, 0, 32 ); 
      memcpy(sBuff, sStart + 8, ( sMsgStartPtr - sStart - 10) );
      mMember= atoi((char*)&sBuff );
   }
   else
   {
      /* do nothing */
   }

   if ( flags_f::Matches( sMsgStartPtr, sStart ) )
   {
      memset( mFlags, 0, 32 ); 
      memcpy( mFlags, sStart + 7, ( sMsgStartPtr - sStart - 9) );
   }
   else
   {
      /* do nothing */
   }

   if ( callerid_f::Matches( sMsgStartPtr, sStart ) )
   {
       memset( mCallerId, 0, 32 ); 
       memcpy( mCallerId, sStart + 10, ( sMsgStartPtr - sStart - 12) );
   }
   else
   {
      /* do nothing */
   }

   if ( calleridname_f::Matches( sMsgStartPtr, sStart ) )
   {
       memset( mCallerIdName, 0, 32 ); 
       memcpy( mCallerIdName, sStart + 14, ( sMsgStartPtr - sStart - 16) );
   }
   else
   {
      /* do nothing */
   }

   if ( count_f::Matches( sMsgStartPtr, sStart ) )
   {
       memset( sBuff, 0, 32 ); 
       memcpy( sBuff, sStart + 7, ( sMsgStartPtr - sStart - 9 ) );
       mCount = atoi( (char*) &sBuff );
   }
   else
   {
      /* do nothing */
   }
   
   return this;
}

CCmdJoin & CCmdJoin::operator = ( const CCmdJoin & aOther )
{
   sprintf( mPrivilege,      "%s", aOther.mPrivilege );
   sprintf( mConferenceName, "%s", aOther.mConferenceName );
   sprintf( mUniqueId,       "%s", aOther.mUniqueId );
   sprintf( mFlags,          "%s", aOther.mFlags );
   sprintf( mChannel,        "%s", aOther.mChannel );
   sprintf( mCallerId,       "%s", aOther.mCallerId );
   sprintf( mCallerIdName,   "%s", aOther.mCallerIdName );
   
   mMember = aOther.mMember;
   mCount  = aOther.mCount;

   return *this;
}

int CCmdJoin::operator < ( const CCmdJoin & aOther )
{
   int sRetCode = 0;
   
   sRetCode = strcmp( mPrivilege, aOther.mPrivilege );

   if( sRetCode < 0 )
   {
      /* do nothing */
   }
   else
   {
      goto notless;
   }
   
   sRetCode = strcmp( mConferenceName, aOther.mConferenceName );
   if( sRetCode < 0 )
   {
      /* do nothing */
   }
   else
   {
      goto notless;
   }
   
   sRetCode = strcmp( mUniqueId, aOther.mUniqueId );
   if( sRetCode < 0 )
   {
      /* do nothing */
   }
   else
   {
      goto notless;
   }
   
   if( mMember< aOther.mMember)
   {
      /* do nothing */
   }
   else
   {
      goto notless;
   }
   
   sRetCode = strcmp( mFlags, aOther.mFlags );
   if( sRetCode < 0 )
   {
      /* do nothing */
   }
   else
   {
      goto notless;
   }
   
   sRetCode = strcmp( mChannel, aOther.mChannel );
   if( sRetCode < 0 )
   {
      /* do nothing */
   }
   else
   {
      goto notless;
   }
   
   sRetCode = strcmp( mCallerId, aOther.mCallerId );
   if( sRetCode < 0 )
   {
      /* do nothing */
   }
   else
   {
      goto notless;
   }

   sRetCode = strcmp( mCallerIdName, aOther.mCallerIdName );
   if( sRetCode < 0 )
   {
      /* do nothing */
   }
   else
   {
      goto notless;
   }
   
   if( mCount < aOther.mCount )
   {
      /* do nothing */
   }
   else
   {
      goto notless;
   }

   return 1;
   
notless:

   return 0;
}

int CCmdJoin::operator > ( const CCmdJoin &aOther )
{
   int sRetCode = 0;
   
   sRetCode = strcmp( mPrivilege, aOther.mPrivilege );
   if( sRetCode > 0 )
   {
      /* do nothing */
   }
   else
   {
      goto notmore;
   }
   
   sRetCode = strcmp( mConferenceName, aOther.mConferenceName );
   if( sRetCode > 0 )
   {
      /* do nothing */
   }
   else
   {
      goto notmore;
   }
   
   sRetCode = strcmp( mUniqueId, aOther.mUniqueId );
   if( sRetCode > 0 )
   {
      /* do nothing */
   }
   else
   {
      goto notmore;
   }
   
   if( mMember> aOther.mMember )
   {
      /* do nothing */
   }
   else
   {
      goto notmore;
   }
   
   sRetCode = strcmp( mFlags, aOther.mFlags );
   if( sRetCode > 0 )
   {
      /* do nothing */
   }
   else
   {
      goto notmore;
   }
   
   sRetCode = strcmp( mChannel, aOther.mChannel );
   if( sRetCode > 0 )
   {
      /* do nothing */
   }
   else
   {
      goto notmore;
   }
   
   sRetCode = strcmp( mCallerId, aOther.mCallerId );
   if( sRetCode > 0 )
   {
      /* do nothing */
   }
   else
   {
      goto notmore;
   }
   
   sRetCode = strcmp( mCallerIdName,aOther.mCallerIdName );
   if( sRetCode > 0 )
   {
      /* do nothing */
   }
   else
   {
      goto notmore;
   }
   
   if( mCount > aOther.mCount )
   {
      /* do nothing */
   }
   else
   {
      goto notmore;
   }

   return 1;
   
notmore:
   
   return 0;
}

int CCmdJoin::operator == ( const CCmdJoin &aOther )
{
   int sRetCode = 0;
    
   sRetCode = strcmp( mPrivilege, aOther.mPrivilege );
   if( sRetCode == 0 )
   {
      /* do nothing */
   }
   else
   {
      goto notequal;
   }

   sRetCode = strcmp( mConferenceName, aOther.mConferenceName );
   if( sRetCode == 0 )
   {
      /* do nothing */
   }
   else
   {
      goto notequal;
   }

   sRetCode = strcmp( mUniqueId, aOther.mUniqueId );
   if( sRetCode == 0 )
   {
      /* do nothing */
   }
   else
   {
      goto notequal;
   }

   if( mMember == aOther.mMember )
   {
      /* do nothing */
   }
   else
   {
      goto notequal;
   }

   sRetCode = strcmp( mFlags, aOther.mFlags );
   if( sRetCode == 0 )
   {
      /* do nothing */
   }
   else
   {
      goto notequal;
   }

   sRetCode = strcmp( mChannel, aOther.mChannel );
   if( sRetCode == 0 )
   {
      /* do nothing */
   }
   else
   {
      goto notequal;
   }

   sRetCode = strcmp( mCallerId, aOther.mCallerId );
   if( sRetCode == 0 )
   {
      /* do nothing */
   }
   else
   {
      goto notequal;
   }

   sRetCode = strcmp( mCallerIdName, aOther.mCallerIdName );
   if( sRetCode == 0 )
   {
      /* do nothing */
   }
   else
   {
      goto notequal;
   }

   if( mCount == aOther.mCount )
   {
      /* do nothing */
   }
   else
   {
      goto notequal;
   }

   return 1;

notequal:

   return 0;
}

int CCmdJoin::event()
{
   char   sChannelList[10][64];
   char   sFilename[256] = {0,};

   int    sRetCode = 0;
   int    sCnt     = 0;
   int    sBufLen  = 10;

   ONLINE sOnline;
   
   // remove called call information.
   // If not removed, call genrated repeatly.
   // swhors@naver.com
   // --start--
   snprintf( sFilename,
             256,
             "%s/%s.call",
             gOutgoingFolder,
             mConferenceName );
   
   remove(sFilename);
   // -- end --
   
   if ( gIsConnected2DB )
   {
      sOnline.mMemberId = mMember;
      sprintf( sOnline.mConference,"%s", mConferenceName );
      sprintf( sOnline.mChannel,   "%s", mChannel );
      sprintf( sOnline.mUniqueId,  "%s", mUniqueId );
      sprintf( sOnline.mNumber,    "%s", mCallerId );
      sprintf( sOnline.mName,      "%s", mCallerIdName );
      online_sql_set_confjoin( &sOnline );
      if ( !strstr( mFlags, "p" ) )
      {
         // play how many people are in the conference
         playcount( mCount, mChannel );
      }
      
      if ( strstr( mFlags, "i" ) )
      {
         if ( sOnline.mMemberId >= 0 )
         {
            online_sql_set_onconf(mConferenceName, 1);
         }
         
         online_sql_get_channellist( mConferenceName,
                                     sChannelList,
                                     &sBufLen,
                                     1 );
         if ( sBufLen > 0 )
         {
            for( sCnt = 0 ; sCnt < sBufLen ; sCnt++ )
            {
                sRetCode = sendCommand( (char*)"Action: Command\r\n"\
                            "Command: conference play sound %s"\
                            " /tmp/%s/%s conf-hasentered\r\n\r\n",
                            sChannelList[sCnt],
                            mConferenceName,
                            mUniqueId );
            }
         }
      }
      
      if ( strstr( mFlags, "q" ) )
      {
         // play enter tone
         sBufLen = 10;

         online_sql_get_channellist( mConferenceName,
                                     sChannelList,
                                     &sBufLen,
                                     1 );
         if( sBufLen > 0 )
         {
            for( sCnt = 0 ; sCnt < sBufLen; sCnt++ )
            {
                sRetCode = sendCommand((char*)"Action: Command\r\n"\
                           "Command: conference play sound %s"\
                           " join mute\r\n\r\n",
                           sChannelList[sCnt]);
            }
         }
      }
      goto event_success;
   }
   goto event_fail;
   
event_success:
   
   return sRetCode;
   
event_fail:
   
   return 0;
}

